package com.demo.jianjunhuang.intentdemo;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnClickListener {

  private Button customer_intent_btn;
  private Button url_intent_btn;
  private static final String TAG = "MainActivity";

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initEvent();
  }

  private void initEvent() {
    customer_intent_btn.setOnClickListener(this);
    url_intent_btn.setOnClickListener(this);
  }

  private void initView() {
    customer_intent_btn = (Button) findViewById(R.id.customer_intent_btn);
    url_intent_btn = (Button) findViewById(R.id.url_intent_btn);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.customer_intent_btn: {
        Intent intent = new Intent("com.demo.jianjunhuang.intentdemo.CustomerIntentActivity");
        //intent.addCategory("");
        startActivityForResult(intent,1);
        break;
      }
      case R.id.url_intent_btn: {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);
        break;
      }
    }
  }

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
      if (resultCode == RESULT_OK) {
        Log.d(TAG, "onActivityResult: "+data.getStringExtra("data"));
      }
    } else {
      Log.d(TAG, "onActivityResult: fail");
    }
  }
}
